let app = getApp()
Component({
    properties: {
        toPage: { // 声明属性
            type: String,
            value: 0
        }
    },
    data: {
        loginCode: '',
        isPrivacyChecked: true,
        isLogin: true
    },
    lifetimes: {
        attached: function () {
            this.init()
        },
    },
    methods: {
        init() {
            // console.log("popup-init")
            if (wx.getStorageSync('userInfo')) {
                // console.log("popup-init-user")
                this.setData({
                    isLogin: true
                })
                return false
            } else {
                // console.log("popup-init-not-user")
                wx.hideTabBar()
                this.setData({
                    isLogin: false
                })
                let that = this
                wx.login({
                    success(resForCode) {
                        that.setData({
                            loginCode: resForCode.code
                        })
                    }
                })
                return true
            }
        },
        openPrivacyContract() {
            wx.openPrivacyContract({
                success: res => {
                    console.log('openPrivacyContract success')
                },
                fail: res => {
                    console.error('openPrivacyContract fail', res)
                }
            })
        },
        onChangeForPrivacy(e) {
            // 获取选中的值
            const selectedValues = e.detail.value;
            // 判断是否选中了隐私政策复选框
            const isPrivacyChecked = selectedValues.includes('privacy');
            // 更新页面的数据
            this.setData({
                isPrivacyChecked: isPrivacyChecked
            });
        },
        onClose() {
            wx.showTabBar()
            this.setData({
                isLogin: true
            })
        },
        exit() {
            wx.exitMiniProgram()
        },
        // 微信直接登录
        wxLogin() {
            if (!this.data.isPrivacyChecked) {
                wx.showToast({
                    title: '请先阅读并同意协议!',
                    duration: 1000,
                    icon: 'none',
                    mask: false
                })
                return
            }
            wx.getUserProfile({
                desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (resForUserInfo) => {
                    // console.log(resForUserInfo)
                    wx.showLoading({
                        title: '登录中',
                    })
                    wx.request({
                        url: app.globalData.url + '/wxLogin',
                        header: {
                            'content-type': 'application/json'
                        },
                        method: 'POST',
                        data: {
                            loginCode: this.data.loginCode,
                            rawData: resForUserInfo.rawData,
                            signature: resForUserInfo.signature
                        },
                        success: (res) => {
                            let userInfo = res.data.data
                            if (!userInfo) {
                                wx.showToast({
                                    title: '密码错误或账号不存在',
                                    icon: 'none'
                                })
                            } else {
                                this.loginSuccess(userInfo)
                            }
                        },
                        fail: (err) => {
                            console.log("失败", err);
                        },
                        // complete: (com) => {

                        // }
                    })

                }
            })
        },
        // 点击登录
        sign(e) {
            if (!this.data.isPrivacyChecked) {
                wx.showToast({
                    title: '请先阅读并同意协议!',
                    duration: 1000,
                    icon: 'none',
                    mask: false
                })
                return
            }
            let UserVO = e.detail.value
            if (!UserVO.phone || !UserVO.password) {
                wx.showToast({
                    title: '请输入完整',
                    icon: 'none'
                })
            } else {
                wx.showLoading({
                    title: '登录中',
                })
                wx.request({
                    url: app.globalData.url + '/sign',
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: 'GET',
                    data: {
                        phone: UserVO.phone,
                        password: UserVO.password
                    },
                    success: (res) => {
                        let userInfo = res.data.data
                        if (!userInfo) {
                            wx.showToast({
                                title: '密码错误或账号不存在',
                                icon: 'none'
                            })
                        } else {
                            this.loginSuccess(userInfo)
                        }
                    },
                    fail: (err) => {
                        console.log("失败", err);
                    },
                    // complete: (com) => {

                    // }
                })
            }


        },
        loginSuccess(userInfo) {
            wx.showTabBar()
            wx.showToast({
                title: '登录成功',
            })
            wx.setStorageSync('userInfo', userInfo);
            app.chooseTheme(userInfo.theme);
            this.setData({
                isLogin: true
            })

            switch (this.data.toPage) {
                case "1":
                    wx.reLaunch({
                        url: '/pages/index/index'
                    });
                    break;
                case "2":
                    wx.reLaunch({
                        url: '/pages/Statistics/Statistics'
                    });
                    break;
                case "3":
                    wx.reLaunch({
                        url: '/pages/oil/oil'
                    });
                    break;
                case "4":
                    wx.reLaunch({
                        url: '/pages/my/my'
                    });
                    break;
                case "5":
                    wx.reLaunch({
                        url: '/pages/compute/compute'
                    });
                    break;
            }
        }

    }
})